package com.donleo.security.controller;


import com.donleo.common.api.CommonResult;
import com.donleo.security.model.Permission;
import com.donleo.security.service.PermissionService;
import com.donleo.security.vo.PermissionVo;
import com.donleo.security.vo.TreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author liangd
 * @since 2020-12-21
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 添加菜单
     */
    @PostMapping("/add")
    public CommonResult add(@RequestBody Permission permission) {
        return permissionService.add(permission);
    }

    /**
     * 批量删除
     */
    @PostMapping("/deletes")
    public CommonResult deletes(Integer[] ids) {
        List<Integer> idsList = Arrays.asList(ids);
        return permissionService.deletes(idsList);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public CommonResult update(@RequestBody Permission permission) {
        Assert.notNull(permission.getId(), "Id is not to be null");
        return permissionService.updatePermisson(permission);
    }

    /**
     * 查询所有菜单
     */
    @GetMapping("/findAll")
    public CommonResult findMenuAll(PermissionVo permissionVo) {
        List<Permission> list = permissionService.findAll(permissionVo);
        long total = permissionService.getCount(permissionVo);
        return CommonResult.success(total, list);
    }

    /**
     * 查询单个
     */
    @GetMapping("/findById")
    public CommonResult findById(Integer id) {
        Permission permission = permissionService.getById(id);
        return CommonResult.success(permission);
    }

    /**
     * 菜单生成树
     */
    @GetMapping("/findMenuTree")
    public CommonResult findMenuTree(Integer type) {
        Map<String, Object> map = permissionService.findMenuTree(type);
        return CommonResult.success(map);
    }


    /**
     * 根据角色编号查权限
     */
    @GetMapping("/findPmsByRoleCode")
    public CommonResult findPmsByRoleCode(String roleCode) {
        List<Permission> list = permissionService.findPmsByRoleCode(roleCode);
        return CommonResult.success(list);
    }


    /**
     * 分配菜单权限
     */
    @PostMapping("/getMenuTreeData")
    public CommonResult getMenuTreeData(@RequestBody List<TreeVo> treeVo,
                                        @RequestParam String roleCode,
                                        @RequestParam Integer type) {
        return permissionService.getMenuTreeData(treeVo, roleCode, type);
    }

    /**
     * 根据用户生成菜单
     */
    @GetMapping("/findUserMenuPermission")
    public CommonResult findUserMenuPermission(String userCode) {
        Map<String,Object> map = permissionService.findUserMenuPermission(userCode);
        return CommonResult.success(map);
    }
}
